package com.ewzj.bigdata.mvp.model

import android.app.Application
import com.ewzj.bigdata.app.config.AppKey
import com.ewzj.bigdata.app.config.netmodel.CommonCache
import com.ewzj.bigdata.app.config.netmodel.CommonService
import com.ewzj.bigdata.app.extensions.json2Body
import com.ewzj.bigdata.app.utils.Apputils
import com.ewzj.bigdata.mvp.contract.HomeFragmentContract
import com.ewzj.bigdata.mvp.model.reqbean.ReqAd
import com.ewzj.bigdata.mvp.model.reqbean.ReqTopicEnter
import com.ewzj.bigdata.mvp.model.reqbean.Reqpagebean
import com.ewzj.bigdata.mvp.model.rspbean.*
import com.google.gson.Gson
import com.jess.arms.integration.IRepositoryManager
import com.jess.arms.mvp.BaseModel
import com.jess.arms.utils.DeviceUtils
import io.reactivex.Observable
import io.rx_cache2.DynamicKey
import io.rx_cache2.EvictDynamicKey
import io.rx_cache2.EvictProvider
import java.util.*
import javax.inject.Inject

/**
 * author:zhangjing
 * 作用:
 * return:
 */
class HomeFragmentModel @Inject constructor(repositoryManager: IRepositoryManager,val mGson:Gson,val application: Application):BaseModel(repositoryManager),HomeFragmentContract.Model {
    val service :CommonService by lazy { repositoryManager.obtainRetrofitService(CommonService::class.java) }
    val cacheservice :CommonCache by lazy { repositoryManager.obtainCacheService(CommonCache::class.java) }
    override fun getHomev3(pagebean: Reqpagebean) :Observable<RspHomeData>{
        var cacheObserable:Observable<RspHomeData> =cacheservice.homev3list(service.homeV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean))).map { t ->t.data  }, DynamicKey(1),EvictDynamicKey(DeviceUtils.hasInternet(application)))
        var netObserable =cacheservice.homev3list(service.homeV3(Apputils.Utils.json2Requestbody(mGson.toJson(pagebean))).map { t ->t.data  }, DynamicKey(pagebean.p),EvictDynamicKey(DeviceUtils.hasInternet(application)))
        return  netObserable
    }
    //    分割数据
    override fun <T> subList(list: List<T>?): List<List<T>> {
        val blockSize = 2
        val lists = ArrayList<List<T>>()
        if (list != null && blockSize > 0) {
            val listSize = list.size
            if (listSize <= blockSize) {
                lists.add(list)
                return lists
            }
            val batchSize = listSize / blockSize
            val remain = listSize % blockSize
            for (i in 0..batchSize - 1) {
                val fromIndex = i * blockSize
                val toIndex = fromIndex + blockSize
                println("fromIndex=$fromIndex, toIndex=$toIndex")
                lists.add(list.subList(fromIndex, toIndex))
            }
            if (remain > 0) {
                println("fromIndex=" + (listSize - remain) + ", toIndex=" + listSize)
                lists.add(list.subList(listSize - remain, listSize))
            }
        }
        return lists
    }

    override fun getAdList(): Observable<BaseData<AdBean>> {
        return service.getAdlist(json2Body(mGson.toJson(ReqAd("2"))))
    }

    override fun gerRecHomelist(page: Reqpagebean): Observable<RspHomeData> {
       page.type_id="1"
       return service.getHomelist(Apputils.Utils.json2Requestbody(mGson.toJson(page))).map { t -> t.data }
    }

    override fun gerNewHomelist(page: Reqpagebean): Observable<RspHomeData> {
        page.type_id="4"
        return service.getHomelist(Apputils.Utils.json2Requestbody(mGson.toJson(page))).map { t -> t.data }
    }

    override fun gerOldHomelist(page: Reqpagebean): Observable<RspHomeData> {
        page.type_id="2"
        return service.getHomelist(Apputils.Utils.json2Requestbody(mGson.toJson(page))).map { t -> t.data }
    }

    override fun gerRentHomelist(page: Reqpagebean): Observable<RspHomeData> {
        page.type_id="3"
        return service.getHomelist(Apputils.Utils.json2Requestbody(mGson.toJson(page))).map { t -> t.data }
    }

    override fun getpopAd(): Observable<BaseData<AdBean>> {
        return service.getAdlist(json2Body(mGson.toJson(ReqAd("3"))))
    }

    override fun gettopicEnter():Observable<BaseData<BaseListData<TopicEnterBean>>> {
        var req=ReqTopicEnter()
        req.postion="1"
        return service.getTopic_Enter(json2Body(mGson.toJson(req)));
    }
}